package com.bs.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.bs.demo.po.Cases;
import com.bs.demo.service.CasesService;
import com.bs.demo.mapper.CasesMapper;
import com.bs.demo.vo.CallDemoVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author admin
 * @description 针对表【cases(流调个案)】的数据库操作Service实现
 * @createDate 2022-09-23 09:27:45
 */
@Service
public class CasesServiceImpl extends ServiceImpl<CasesMapper, Cases>
        implements CasesService {

    @Resource
    private CasesMapper casesMapper;

    @Override
    public void write() throws InterruptedException {
//        ExecutorService executorService = new ThreadPoolExecutor(
//                5,
//                5, 0L,
//                TimeUnit.MILLISECONDS,
//                new LinkedBlockingDeque<>(10),
//                Executors.defaultThreadFactory(),
//                new ThreadPoolExecutor.DiscardPolicy());
        ExecutorService executorService = Executors.newCachedThreadPool();
        Integer count = Math.toIntExact(casesMapper.selectCount(new LambdaQueryWrapper<>()));
        Integer perCount = 100;
        Integer times = count / perCount;
        if (count % perCount != 0) {
            times = times + 1;
        }
        List<Callable<List<Cases>>> callableList = new LinkedList<>();
        for (int i = 0; i < times; i++) {
            CallDemoVo callDemoVo = new CallDemoVo();
            callDemoVo.setStartIndex(i * perCount);
            if ((count - i * perCount) > perCount) {
                callDemoVo.setLength(perCount);
            } else {
                callDemoVo.setLength(count - i * perCount);
            }
            callDemoVo.setCasesMapper(casesMapper);
            callableList.add(callDemoVo);
        }
        List<Future<List<Cases>>> futures = executorService.invokeAll(callableList, 10, TimeUnit.SECONDS);
        executorService.shutdown();
    }
}




